package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("moduletypeAction")
public class ModuletypeAction extends MyAction {
	private static final long serialVersionUID = 1L;
	private ModuletypeService service;

	public ModuletypeService getService() {
		return service;
	}

	@Resource
	public void setService(ModuletypeService service) {
		this.service = service;
	}

	public String list() throws IOException {
		GL.info("user.list()函数  ");
		beginReqParam();

		List<T_moduletype> tabObjs = service.queryAll(start, limit);

		jsonString = "{totalProperty:" + service.queryCount() + ",root:[";
		int i = 0;
		for (T_moduletype to : tabObjs) {

			if (i != 0 && i != tabObjs.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{mod_type:'" + to.getMod_type() + "',mod_typename:'" + to.getMod_typename()
					+ "',mod_producer:'" + to.getMod_producer() + "',mod_memo:'" + to.getMod_memo() + "'}";
			jsonString = jsonString + t;
		}
		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();
		// ApplicationContext ac = new
		// ClassPathXmlApplicationContext("beans.xml");
		// List<T_unit> complist = service.queryAll();
		JSONArray array = JSONArray.fromObject(service.queryAll());
		out.write(array.toString());
		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		T_moduletype tabObj = service.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_mod_typename:'" + tabObj.getMod_typename() + "',f_mod_producer:'"
				+ tabObj.getMod_producer() + "',f_mod_memo:'" + tabObj.getMod_memo() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_mod_typename = request.getParameter("f_mod_typename");
		String f_mod_producer = request.getParameter("f_mod_producer");
		String f_mod_memo = request.getParameter("f_mod_memo");

		T_moduletype to = new T_moduletype();
		to.setMod_typename(f_mod_typename);
		to.setMod_producer(f_mod_producer);
		to.setMod_memo(f_mod_memo);

		service.add(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("mod_type");
			service.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();

		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		String f_mod_typename = request.getParameter("f_mod_typename");
		String f_mod_producer = request.getParameter("f_mod_producer");
		String f_mod_memo = request.getParameter("f_mod_memo");

		T_moduletype to = service.query(Integer.parseInt(p_id));
		to.setMod_typename(f_mod_typename);
		to.setMod_producer(f_mod_producer);
		to.setMod_memo(f_mod_memo);

		service.update(to);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
